package request1_i2p;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;



public class COrderSubscriber implements Observer {
    private List<Merchant> subscribers=new ArrayList<>();
    public COrderSubscriber(List<Merchant> subscribers){
        this.subscribers=subscribers;
    }

    public List<Merchant> getSubscribers() {
        return subscribers;
    }

    public void setSubscribers(List<Merchant> subscribers) {
        this.subscribers = subscribers;
    }

    @Override
    public void update(Observable o, Object arg) {
        COrderPublisher dispatch=(COrderPublisher) o;
        COrderEvent orderEvent=(COrderEvent)arg;
        Consumer consumer=dispatch.getConsumer();
        Merchant final_rider=null;
        double min=9999;
        String ID=orderEvent.getMerchant().getName();
        for(Merchant rider:subscribers){
            if(rider.getName()==ID){
                Double distance=Math.sqrt(Math.pow((rider.getLocation()[0]-consumer.getLocation()[0]),2)+Math.pow((rider.getLocation()[1]-consumer.getLocation()[1]),2));
                System.out.println("商家"+rider.getName()+"您好！");
                System.out.println("接收到来自"+dispatch.getConsumer().getName()+"的订单任务");
                System.out.println("用户距您："+distance+"公里");
                System.out.println("-----------------------------");
                final_rider=rider;
            }

        }
        orderEvent.setMerchant(final_rider);
        System.out.println("事件分发完毕，最终分配的商家为："+final_rider.getName());

    }
}
